package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.QuestionQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.article.service.IQuestionTagService;
import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.vo.QuestionStatisVO;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * @author 名字
 * @company 公司
 * @create 2019-09-25 22:13
 */
@Controller
@RequestMapping("wenda")
public class QuestionController {

    @Reference
    private IQuestionService questionService;

    @Reference
    private IQuestionTagService questionTagService;

    @Reference
    private IAnswerService answerService;

    @Reference
    private IQuestionRedisService questionRedisService;

    @Reference
    private IDestinationService destinationService;

    @RequestMapping("detail")
    public String detail(Model model,Long id, @UserParam UserInfo userInfo) {
        //增加访问量
        questionRedisService.viewNumIncrease(id,1);
        QuestionStatisVO statisVo = questionRedisService.getStatisVo(id);

        model.addAttribute("vo",statisVo);


        //查询单个
        Question question = questionService.get(id);

        //查询该问答下的所有回答
        List<Answer> answers = answerService.queryByQId(question.getId());
        question.setAnswers(answers);

        model.addAttribute("q",question);

        if(userInfo != null){
            boolean isAttention = questionRedisService.isAttention(id,userInfo.getId());
            model.addAttribute("isAttention",isAttention);
        }
        return "wenda/wendaDetail";
    }

    @RequestMapping("")
    public String index(Model model,@ModelAttribute("qo") QuestionQuery qo) {
        //查询全部
        PageInfo pageInfo = questionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "wenda/index";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequireLogin
    public AJaxResult saveOrUpdate(Question question, @UserParam UserInfo userInfo, Long[] tags) {
        //1.设置提问人
        question.setAuthor(userInfo);
        //2.添加
        questionService.saveOrUpdate(question,tags);
        return new AJaxResult();
    }

    @RequestMapping("input")
    @RequireLogin
    public String input(Model model,Long id) {
        if(id != null){
            Question question = questionService.get(id);
            QuestionContent questionContent = questionService.getContent(id);
            question.setQuestionContent(questionContent);
            model.addAttribute("question",question);
        }
        //共享目的地
        List<Destination> list = destinationService.list();
        model.addAttribute("dests",list);

        //共享标签
        List<QuestionTag> questionTags = questionTagService.query();
        model.addAttribute("tags",questionTags);

        return "wenda/public";
    }


    @RequestMapping("/attention")
    @ResponseBody
    public Object attention(Long qid, @UserParam UserInfo userInfo){
        if (userInfo == null) {
            AJaxResult aJaxResult = new AJaxResult(false, "");
            aJaxResult.setCode(102);
            return aJaxResult;
        }
        AJaxResult aJaxResult = new AJaxResult();
        boolean ret = questionRedisService.attention(qid,userInfo.getId());
        if(ret){
            //加关注数
            questionRedisService.attentionNum(qid);
        } else{
            aJaxResult.setSuccess(false);
            aJaxResult.setCode(104);
        }
        return aJaxResult.addData(questionRedisService.getStatisVo(qid).getAttentionnum());
    }
}
